package constant.milk.cashbook.auto;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import android.content.Context;

import constant.milk.cashbook.R;
import constant.milk.cashbook.bean.AutoMainData;
import constant.milk.cashbook.bean.BankData;
import constant.milk.cashbook.bean.CreditData;
import constant.milk.cashbook.bean.HalbuData;
import constant.milk.cashbook.bean.InOutData;
import constant.milk.cashbook.bean.PhoneData;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.database.DbAdapter;
import constant.milk.cashbook.database.DbInsertQuery;
import constant.milk.cashbook.database.DbUpdateQuery;
import constant.milk.cashbook.insert.InsertActivity;
import constant.milk.cashbook.util.MilkManager;
import constant.milk.cashbook.util.MilkUtil;

public class AutoManager
{
	private static AutoManager autoManager = new AutoManager();
	
	private int monthCount;
	private int weekdayCount;
	private int creditCardCount;
	private int phoneCount;
	private int halbuCount;
	
	public static AutoManager getInstance()
	{
		return autoManager;
	}
	
	public String start(Context context, DbAdapter db)
	{
		String massage = "";
		
		monthCount = 0;
		weekdayCount = 0;
		creditCardCount = 0;
		phoneCount = 0;
		halbuCount = 0;
		
		startMonth(context, db, 0);
		startMonth(context, db, 1);
		startMonth(context, db, 2);
		
		startWeekday(context, db, 0);
		startWeekday(context, db, 1);
		startWeekday(context, db, 2);
		
		startCreditCard(context, db);
		startPhone(context, db);
		
		startHalbu(context, db);
		
		if (monthCount != 0)
		{
			massage += "월간 자동 " + monthCount;
		}
		
		if (weekdayCount != 0)
		{
			if (massage != "")
			{
				massage += ", ";
			}
			
			massage += "주간 자동 " + weekdayCount;
		}
		
		if (creditCardCount != 0)
		{
			if (massage != "")
			{
				massage += ", ";
			}
			
			massage += "신용카드 대금결제  " + creditCardCount;
		}
		
		if (phoneCount != 0)
		{
			if (massage != "")
			{
				massage += ", ";
			}
			
			massage += "휴대폰 대금결제 " + phoneCount;
		}
		
		if (halbuCount != 0)
		{
			if (massage != "")
			{
				massage += ", ";
			}
			
			massage += "할부 " + halbuCount;
		}
		
		if (massage != "")
		{
			massage += " 가 입력 되었습니다.";
		}
		
		return massage;
	}

	private void startMonth(Context context, DbAdapter db, int flag)
	{
		DBSelectQuery select = new DBSelectQuery(context, db);
		
		ArrayList<AutoMainData> list = new ArrayList<AutoMainData>();
		
		list = select.getAutoMonthList(flag);
		
		Calendar cal = Calendar.getInstance();
		
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		
		String date = MilkUtil.getDateDB2(year, month+1);
		
		for (int i = 0; i < list.size(); i++)
		{
			while (true)
			{
				AutoMainData data = select.getAutoId(list.get(i).getId());
				
				int [] dates = MilkUtil.getDateSet(data.getSigndate());
				
				if (Integer.parseInt(date) <= Integer.parseInt(MilkUtil.getDateDB2(dates[0], dates[1])))
				{
					break;
				}
				
				setInOutDataMonth(context, db, data, flag);
			}
		}
	}

	private void setInOutDataMonth(Context context, DbAdapter db, AutoMainData data, int flag)
	{
		DbInsertQuery query = null;
		
		try
		{
			query = new DbInsertQuery(context, db);
			
			query.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			
			map.put("type", flag + "");
			
			map.put("kind", "0");
			
			map.put("asset", data.getAsset());
			map.put("name", data.getName());
			map.put("maincategory", data.getMaincategory());
			map.put("subcategory", data.getSubcategory());

//			Calendar cal = Calendar.getInstance();
//			int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//			
//			int [] dates = MilkUtil.getDateSet(data.getSigndate());
//			
//			if (day >= Integer.parseInt(data.getAutodate()))
//			{
//				map.put("date", MilkUtil.getDateDB(
//						dates[0], dates[1], Integer.parseInt(data.getAutodate())));
//			}
//			else
//			{
//				map.put("date", MilkUtil.getDateDB(
//						dates[0], dates[1], day));
//			}
			
			map.put("date", getMaxDate(data.getSigndate(), data.getAutodate()));
			
			map.put("ampm", data.getAmpm());
			map.put("timehour", data.getTimehour());
			map.put("timeminute", data.getTimeminute());
			map.put("money", data.getMoney());
			map.put("memo", data.getMemo());
			
			if (flag == 2)
			{
				map.put("subasset", data.getSubasset());
				map.put("subname", data.getSubname());
			}
			
			query.setInOut(map);
			
			setRollBank(context, db, data, flag);
			
			setResultSignDateMonth(context, db, data);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
	private void startWeekday(Context context, DbAdapter db, int flag)
	{
		DBSelectQuery select = new DBSelectQuery(context, db);
		
		ArrayList<AutoMainData> list = new ArrayList<AutoMainData>();
		
		list = select.getAutoWeekdayList(flag);
		
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, -7);
		
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		int day = cal.get(Calendar.DAY_OF_MONTH);
		
		String date = MilkUtil.getDateDB(year, month+1, day);
		
		for (int i = 0; i < list.size(); i++)
		{
			while (true)
			{
				AutoMainData data = select.getAutoId(list.get(i).getId());
				
				int [] dates = MilkUtil.getDateSet(data.getSigndate());
				
				if (Integer.parseInt(date) < Integer.parseInt(MilkUtil.getDateDB(dates[0], dates[1], dates[2])))
				{
					break;
				}
				
				setInOutDataWeekday(context, db, data, flag);
			}
		}
	}
	
	private void setInOutDataWeekday(Context context, DbAdapter db, AutoMainData data, int flag)
	{
		DbInsertQuery query = null;
		
		try
		{
			query = new DbInsertQuery(context, db);
			
			query.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			
			map.put("type", flag + "");
			
			map.put("kind", "0");
			
			map.put("asset", data.getAsset());
			map.put("name", data.getName());
			map.put("maincategory", data.getMaincategory());
			map.put("subcategory", data.getSubcategory());

			int [] dates = MilkUtil.getDateSet(data.getSigndate());
			
			Calendar cal = Calendar.getInstance();
			cal.set(dates[0], dates[1]-1, dates[2]);
			cal.add(Calendar.DAY_OF_WEEK, 7);
			
			map.put("date", MilkUtil.getDateDB(
					cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH)));
			
			map.put("ampm", data.getAmpm());
			map.put("timehour", data.getTimehour());
			map.put("timeminute", data.getTimeminute());
			map.put("money", data.getMoney());
			map.put("memo", data.getMemo());
			
			if (flag == 2)
			{
				map.put("subasset", data.getSubasset());
				map.put("subname", data.getSubname());
			}
			
			query.setInOut(map);
			
			setRollBank(context, db, data, flag);
			
			setResultSignDateWeekday(context, db, data);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
	private void setRollBank(Context context, DbAdapter db, AutoMainData data, int flag) throws Exception
	{
		MilkManager assetManager = new MilkManager(context, db);
		
		if (flag == 0)
		{
			assetManager.setAssetMoney(assetManager.getAssetId(data.getAsset()), data.getName(),
					data.getMoney(), 0);
		}
		else if (flag == 1)
		{
			assetManager.setAssetMoney(assetManager.getAssetId(data.getAsset()), data.getName(),
					data.getMoney(), 1);
		}
		else if(flag == 2)
		{
			assetManager.setAssetMoney(assetManager.getAssetId(data.getAsset()), data.getName(),
					data.getMoney(), 0);
			
			assetManager.setAssetMoney(assetManager.getAssetId(data.getSubasset()), data.getSubname(),
					data.getMoney(), 1);
		}
	}
	
	private void setResultSignDateMonth(Context context, DbAdapter db, AutoMainData data) throws Exception
	{
		DbUpdateQuery update = new DbUpdateQuery(context, db);
		
		int [] dates = MilkUtil.getDateSet(data.getSigndate());
		
		Calendar cal = Calendar.getInstance();
		cal.set(dates[0], dates[1]-1, dates[2]);
		
		cal.add(Calendar.MONTH, 1);
		
		HashMap<String, String> map = new HashMap<String, String>();
		
		map.put("signdate", MilkUtil.getDateDB(
				cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH)));
		
		update.setAutoMainUpdate(map, data.getId());
		
		monthCount++;
	}
	
	private void setResultSignDateWeekday(Context context, DbAdapter db, AutoMainData data) throws Exception
	{
		DbUpdateQuery update = new DbUpdateQuery(context, db);
		
		int [] dates = MilkUtil.getDateSet(data.getSigndate());
		
		Calendar cal = Calendar.getInstance();
		cal.set(dates[0], dates[1]-1, dates[2]);
		
		cal.add(Calendar.DAY_OF_MONTH, 7);
		
		HashMap<String, String> map = new HashMap<String, String>();
		
		map.put("signdate", MilkUtil.getDateDB(
				cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH)));
		
		update.setAutoMainUpdate(map, data.getId());
		
		weekdayCount++;
	}
	
	private void startCreditCard(Context context, DbAdapter db)
	{
		DBSelectQuery select = new DBSelectQuery(context, db);
		
		ArrayList<CreditData> list = select.getCreditListAuto();
		
		Calendar cal = Calendar.getInstance();
		
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		
		String date = MilkUtil.getDateDB2(year, month+1);
		
		for (int i = 0; i < list.size(); i++)
		{
			while (true)
			{
				CreditData data = select.getCreditId(list.get(i).getId());
				
				int [] dates = MilkUtil.getDateSet(data.getSigndate());
				
				if (Integer.parseInt(date) <= Integer.parseInt(MilkUtil.getDateDB2(dates[0], dates[1])))
				{
					break;
				}
				
				setInOutDataCreditCardResult(context, db, data);
			}
		}
	}
	
	private void setInOutDataCreditCardResult(Context context, DbAdapter db, CreditData data)
	{
		DbInsertQuery query = null;
		DBSelectQuery select = null;
		
		try
		{
			query = new DbInsertQuery(context, db);
			select = new DBSelectQuery(context, db);
			
			query.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			
			map.put("type", "8");
			
			map.put("kind", "0");
			
			MilkManager milkManager = new MilkManager(context, db);
			
			map.put("asset", milkManager.getAssetStr(context.getString(R.string.creditCardAsset)));
			map.put("name", data.getName());
			map.put("maincategory", "대금처리");
			map.put("subcategory", "신용카드");

//			Calendar cal = Calendar.getInstance();
//			int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//			
//			int [] dates = MilkUtil.getDateSet(data.getSigndate());
//			
//			if (day >= Integer.parseInt(data.getResultdate()))
//			{
//				map.put("date", MilkUtil.getDateDB(
//						dates[0], dates[1], Integer.parseInt(data.getResultdate())));
//			}
//			else
//			{
//				map.put("date", MilkUtil.getDateDB(
//						dates[0], dates[1], day));
//			}
			
			map.put("date", getMaxDate(data.getSigndate(), data.getResultdate()));
			
			map.put("ampm", Calendar.PM + "");
			map.put("timehour", "5");
			map.put("timeminute", "0");
			
			String [] termDates = milkManager.getTermDate(data.getTermdate(), data.getResultdate());
			ArrayList<InOutData> inOutList = select.getInOutList(4, "6", "", "", "", "", termDates[0], termDates[1]);
			String money = milkManager.getSumInOutMoney(inOutList, "3");
			map.put("money", money);
			
			BankData bankData = select.getBankId(data.getLinkid());
			map.put("subasset", milkManager.getAssetStr(context.getString(R.string.bankAsset)));
			map.put("subname", bankData.getName());
			
			map.put("memo", data.getMemo());
			
			if (Integer.parseInt(money) > 0)
			{
				query.setInOut(map);
				
				setCreditCardResultRollBank(context, db, data, money);
			}
			
			setResultCreditCard(context, db, data, money);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
	private void setCreditCardResultRollBank(
			Context context, DbAdapter db, CreditData data, String money) throws Exception
	{
		DBSelectQuery select = new DBSelectQuery(context, db);
		MilkManager milkManager = new MilkManager(context, db);
		
		milkManager.setAssetMoney(context.getString(R.string.creditCardAsset), 
				data.getName(),	money, 1);
		
		BankData bankData = select.getBankId(data.getLinkid());
		
		milkManager.setAssetMoney(context.getString(R.string.bankAsset), 
				bankData.getName(),	money, 0);
		
	}
	
	private void setResultCreditCard(Context context, DbAdapter db, CreditData data, String money) throws Exception
	{
		DbUpdateQuery update = new DbUpdateQuery(context, db);
		
		int [] dates = MilkUtil.getDateSet(data.getSigndate());
		
		Calendar cal = Calendar.getInstance();
		cal.set(dates[0], dates[1]-1, dates[2]);
		
		cal.add(Calendar.MONTH, 1);
		
		HashMap<String, String> map = new HashMap<String, String>();
		
		map.put("signdate", MilkUtil.getDateDB(
				cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH)));
		
		update.setCreditCardUpdate(map, data.getId());
		
		if (Integer.parseInt(money) > 0)
		{
			creditCardCount++;
		}
	}
	
	private void startPhone(Context context, DbAdapter db)
	{
		DBSelectQuery select = new DBSelectQuery(context, db);
		
		ArrayList<PhoneData> list = select.getPhoneListAuto();
		
		Calendar cal = Calendar.getInstance();
		
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		
		String date = MilkUtil.getDateDB2(year, month+1);
		
		for (int i = 0; i < list.size(); i++)
		{
			while (true)
			{
				PhoneData data = select.getPhoneId(list.get(i).getId());
				
				int [] dates = MilkUtil.getDateSet(data.getSigndate());
				
				if (Integer.parseInt(date) <= Integer.parseInt(MilkUtil.getDateDB2(dates[0], dates[1])))
				{
					break;
				}
				
				setInOutDataPhoneResult(context, db, data);
			}
		}
	}
	
	private void setInOutDataPhoneResult(Context context, DbAdapter db, PhoneData data)
	{
		DbInsertQuery query = null;
		DBSelectQuery select = null;
		
		try
		{
			query = new DbInsertQuery(context, db);
			select = new DBSelectQuery(context, db);
			
			query.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			
			map.put("type", "8");
			
			map.put("kind", "0");
			
			MilkManager milkManager = new MilkManager(context, db);
			
			map.put("asset", milkManager.getAssetStr(context.getString(R.string.phoneAsset)));
			map.put("name", data.getName());
			map.put("maincategory", "대금처리");
			map.put("subcategory", "핸드폰");

//			Calendar cal = Calendar.getInstance();
//			int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//			
//			int [] dates = MilkUtil.getDateSet(data.getSigndate());
//			
//			if (day >= Integer.parseInt(data.getResultdate()))
//			{
//				map.put("date", MilkUtil.getDateDB(
//						dates[0], dates[1], Integer.parseInt(data.getResultdate())));
//			}
//			else
//			{
//				map.put("date", MilkUtil.getDateDB(
//						dates[0], dates[1], day));
//			}
			
			map.put("date", getMaxDate(data.getSigndate(), data.getResultdate()));
			
			map.put("ampm", Calendar.PM + "");
			map.put("timehour", "5");
			map.put("timeminute", "0");
			
			String [] termDates = milkManager.getTermDate(data.getTermdate(), data.getResultdate());
			ArrayList<InOutData> inOutList = select.getInOutList(5, "6", "", "", "", "", termDates[0], termDates[1]);
			String money = milkManager.getSumInOutMoney(inOutList, "4");
			map.put("money", money);
			
			if (data.getAddflag().equals("0"))
			{
				BankData bankData = select.getBankId(data.getLinkid());
				map.put("subasset", milkManager.getAssetStr(context.getString(R.string.bankAsset)));
				map.put("subname", bankData.getName());
			}
			else
			{
				CreditData creditData = select.getCreditId(data.getLinkid());
				map.put("subasset", milkManager.getAssetStr(context.getString(R.string.creditCardAsset)));
				map.put("subname", creditData.getName());
			}
			
			map.put("memo", data.getMemo());
			
			if (Integer.parseInt(money) > 0)
			{
				query.setInOut(map);
				
				setPhoneResultRollBank(context, db, data, money);
			}
			
			setResultPhone(context, db, data, money);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
	private void setPhoneResultRollBank(
			Context context, DbAdapter db, PhoneData data, String money) throws Exception
	{
		DBSelectQuery select = new DBSelectQuery(context, db);
		MilkManager milkManager = new MilkManager(context, db);
		
		milkManager.setAssetMoney(context.getString(R.string.phoneAsset), 
				data.getName(),	money, 1);
		
		if (data.getAddflag().equals("0"))
		{
			BankData bankData = select.getBankId(data.getLinkid());
			
			milkManager.setAssetMoney(context.getString(R.string.bankAsset), 
					bankData.getName(),	money, 0);
		}
		else
		{
			CreditData creditData = select.getCreditId(data.getLinkid());
			
			milkManager.setAssetMoney(context.getString(R.string.creditCardAsset), 
					creditData.getName(), money, 0);
		}
	}
	
	private void setResultPhone(Context context, DbAdapter db, PhoneData data, String money) throws Exception
	{
		DbUpdateQuery update = new DbUpdateQuery(context, db);
		
		int [] dates = MilkUtil.getDateSet(data.getSigndate());
		
		Calendar cal = Calendar.getInstance();
		cal.set(dates[0], dates[1]-1, dates[2]);
		
		cal.add(Calendar.MONTH, 1);
		
		HashMap<String, String> map = new HashMap<String, String>();
		
		map.put("signdate", MilkUtil.getDateDB(
				cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH)));
		
		update.setPhoneUpdate(map, data.getId());
		
		if (Integer.parseInt(money) > 0)
		{
			phoneCount++;
		}
	}
	
	private void startHalbu(Context context, DbAdapter db)
	{
		DBSelectQuery select = new DBSelectQuery(context, db);
		
		ArrayList<HalbuData> list = new ArrayList<HalbuData>();
		
		list = select.getHalbuNoList();
		
		Calendar cal = Calendar.getInstance();
		
		int year = cal.get(Calendar.YEAR);
		int month = cal.get(Calendar.MONTH);
		
		String date = MilkUtil.getDateDB2(year, month+1);
		
		for (int i = 0; i < list.size(); i++)
		{
			while (true)
			{
				HalbuData data = select.getHalbuId(list.get(i).getId());
				
				int [] dates = MilkUtil.getDateSet(data.getSigndate());
				
				if (Integer.parseInt(date) <= Integer.parseInt(MilkUtil.getDateDB2(dates[0], dates[1])))
				{
					break;
				}
				
				if (Integer.parseInt(list.get(i).getHalbucount()) <= Integer.parseInt(data.getResultcount()))
				{
					break;
				}
				
				setInOutDataHalbu(context, db, data);
			}
		}
	}
	
	private void setInOutDataHalbu(Context context, DbAdapter db, HalbuData data)
	{
		DbInsertQuery query = null;
		
		try
		{
			query = new DbInsertQuery(context, db);
			
			query.setBeginTansaction();
			
			HashMap<String, String> map = new HashMap<String, String>();
			
			map.put("type", data.getType());
			
			map.put("kind", "0");
			
			map.put("asset", data.getAsset());
			map.put("name", data.getName());
			map.put("maincategory", data.getMaincategory());
			map.put("subcategory", data.getSubcategory());

//			Calendar cal = Calendar.getInstance();
//			int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
//			
//			int [] dates = MilkUtil.getDateSet(data.getSigndate());
//			
//			if (day >= Integer.parseInt(data.getAutodate()))
//			{
//				map.put("date", MilkUtil.getDateDB(
//						dates[0], dates[1], Integer.parseInt(data.getAutodate())));
//			}
//			else
//			{
//				map.put("date", MilkUtil.getDateDB(
//						dates[0], dates[1], day));
//			}
			
			map.put("date", getMaxDate(data.getSigndate(), data.getAutodate()));
			
			map.put("ampm", data.getAmpm());
			map.put("timehour", data.getTimehour());
			map.put("timeminute", data.getTimeminute());
			map.put("money", data.getMoney());
			map.put("memo", data.getMemo());
			
			query.setInOut(map);
			
			setHalbuRollBank(context, db, data);
			
			setResultSignDateHalbu(context, db, data);
			
			query.setSuccessful();
		} catch (Exception e)
		{
			e.printStackTrace();
		} finally {
			try
			{
				query.setEndTansaction();
			} catch (Exception e)
			{
				e.printStackTrace();
			}
		}
	}
	
	private void setHalbuRollBank(Context context, DbAdapter db, HalbuData data) throws Exception
	{
		MilkManager assetManager = new MilkManager(context, db);
		
		assetManager.setAssetMoney(assetManager.getAssetId(data.getAsset()), data.getName(),
				data.getMoney(), 0);
	}
	
	private void setResultSignDateHalbu(Context context, DbAdapter db, HalbuData data) throws Exception
	{
		DbUpdateQuery update = new DbUpdateQuery(context, db);
		
		int [] dates = MilkUtil.getDateSet(data.getSigndate());
		
		Calendar cal = Calendar.getInstance();
		cal.set(dates[0], dates[1]-1, dates[2]);
		
		cal.add(Calendar.MONTH, 1);
		
		HashMap<String, String> map = new HashMap<String, String>();
		
		map.put("signdate", MilkUtil.getDateDB(
				cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1, cal.get(Calendar.DAY_OF_MONTH)));
		
		int count = Integer.parseInt(data.getResultcount());
		
		count = count + 1;
		
		map.put("resultcount", count + "");
		
		update.setHalbuUpdate(map, data.getId());
		
		halbuCount++;
	}
	
	private String getMaxDate(String signDate, String autoDate)
	{
		Calendar cal = Calendar.getInstance();
		int day = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		
		int [] dates = MilkUtil.getDateSet(signDate);
		
		Calendar cal2 = Calendar.getInstance();
		cal2.set(dates[0], dates[1]-1, dates[2]);
		cal2.add(Calendar.MONTH, 1);
		
		if (day >= Integer.parseInt(autoDate))
		{
			return MilkUtil.getDateDB(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH)+1, Integer.parseInt(autoDate));
		}
		else
		{
			return MilkUtil.getDateDB(cal2.get(Calendar.YEAR), cal2.get(Calendar.MONTH)+1, day);
		}
	}
}
